<?php

declare(strict_types=1);

namespace app\model;

use app\model\base\BaseCouponBrandCateRelationModel;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $cate_id 分类id
 * @property int $brand_id 品牌id
 */
class CouponBrandCateRelation extends BaseCouponBrandCateRelationModel
{
    //添加关联
    public static function addRelation($data,$scene){
            $validate = new \app\admin\validate\Coupon\Relation();
            $result = $validate->scene($scene)->check($data);
            if(!$result) return fail($validate->getError());
            $relation=new self();
            $save=[];
            foreach ($data['brand_ids'] as $nk => $nv) {
                      $attr = [];
                      $attr['cate_id'] = $data['cate_id'];
                      $attr['brand_id'] = $nv;
                      $info = self::query()->where('cate_id', $attr['cate_id'])->where('brand_id', $attr['brand_id'])->find();
                      if (!$info) {
                          $save[] = $attr;
                      }
            }
            //dd($save);
            if(!empty($save))  $relation->saveAll($save);
            return success("添加成功！");
    }

    //删除关联
    public static function delRelation($data,$scene){
            $validate = new \app\admin\validate\Coupon\Relation();
            $result = $validate->scene($scene)->check($data);
            if(!$result) return fail($validate->getError());
             self::where($data)->delete();
             return success("删除成功！");
    }
}
